import pyfits
import sys

def update(filename,hd1):
    f = pyfits.open(filename)
    hd0 = f[0].header
    for card,val in hd1.items():
      if card.startswith('WV'):
          hd0[card] = val
    pyfits.update(filename,f[0].data,hd0)
    f.close()

usage = """
Usage: hedit [arcfile] [one or more fits files]

Copies the wavelength solution header cards from the arcfile to the
target fits files.

Example:

hedit ../Arc-044.fits Err-044.fits Flux-044.fits s2n-044.fits
"""

try:
    arcname = sys.argv[1]
    filenames = sys.argv[2:]
except:
    print usage
    sys.exit()

f = pyfits.open(arcname)
hd1 = f[0].header
f.close()

for filename in filenames:
    print 'Updating', filename
    update(filename, hd1)
